#include <stdlib.h>
#include <string.h>
#include "srkrt_method.h"
#include "srkrt_disp_gui_callbacks.h"
#include "srkrt_disp_gui.h"

int main(int argc, char *argv[]) {
	
	/*gestion affichage */
	GtkWidget* VBox;
	GtkWidget* fixed;
	GtkWidget* Table = NULL;
	/* affichage du menu */
	GtkWidget* MenuBar; /* barre de menu */
	GtkWidget* Menu;
	GtkWidget* subMenu;
	GtkWidget* MenuItem;
	GSList* pList; /* liste pour le choix du niveau et du theme */
	#ifdef NETWORK
	GtkWidget* listView; /* gestion du chat */
	GtkWidget* scrollbar; /* "ascenseur" pour le chat */
	GtkTreeViewColumn* column;
	GtkCellRenderer* cellRenderer;
	#endif
	char tmp[100];


	/* variables de jeu */
	Global global;
	
	#ifdef NETWORK
	/* variable Ivy */
	char *bus=getenv("IVYBUS");
	#endif
	
	/* initialisation de la structure Globale */
	#ifdef NETWORK
	global.game.vs = ON_LINE;
	#else
	global.game.vs = HUMAN;
	#endif

  #ifdef SERVER
  strcpy(global.name, "Joueur 1");
  #endif

	#ifdef CLIENT
	global.game = srkrt_create(PLAYER_2);
	strcpy(global.name, "Joueur 2");
	#else
	global.game = srkrt_create(PLAYER_1);
	#endif
	
	global.game.level = LEVEL_EASY;
  
	sprintf(global.pawnPlayer1, "%s%s", IMAGES_PATH, "/pion2.png");
  sprintf(global.pawnPlayer1Highlight, "%s%s", IMAGES_PATH, "/pion2_highlight.png");
  sprintf(global.pawnPlayer2, "%s%s", IMAGES_PATH, "/pion3.png");
  sprintf(global.pawnPlayer2Highlight, "%s%s", IMAGES_PATH, "/pion3_highlight.png");
  sprintf(global.background, "%s%s", IMAGES_PATH, "/fond1.jpg");

	global.move = move_create(0,0,0,0,false);
	global.choice = (gint)1;

	/* Initialisation du random */
	srand(time(NULL)) ;
	
	/* initialisation de gtk */
	gtk_init(&argc, &argv);

	/*creation de la fenetre */
	global.mainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	/* nom de la fenetre */
	#ifdef SERVER
	gtk_window_set_title(GTK_WINDOW(global.mainWindow), "Surakarta Online - serveur");
	#else
	#ifdef CLIENT
	gtk_window_set_title(GTK_WINDOW(global.mainWindow), "Surakarta Online - client");
	#else
	gtk_window_set_title(GTK_WINDOW(global.mainWindow), "Surakarta");
	#endif
	#endif
	/* position de la fenetre */
	gtk_window_set_position(GTK_WINDOW(global.mainWindow), GTK_WIN_POS_CENTER) ;
	
	/* taille de la fenetre */
	gtk_window_set_default_size(GTK_WINDOW(global.mainWindow), X_WINDOWS, Y_WINDOWS);
	/* on rend la fenetre NON-redimenionnable */
	gtk_window_set_resizable(GTK_WINDOW(global.mainWindow), FALSE);


	/* on cree une boite vertivale permettant d'empiler les widgets les uns sous les autres */
	VBox = gtk_vbox_new(FALSE, 0); /* FALSE = les widgets ne font pas la meme taille */

	/* creation du fixed qui contient le fond */
	fixed = gtk_fixed_new();
	
	/* Chargement de l'image de fond a partir d'un fichier */
	global.imageBackground = gtk_image_new_from_file(global.background);
	gtk_fixed_put(GTK_FIXED(fixed), global.imageBackground, 0, 0);

  sprintf(tmp, "%s%s", IMAGES_PATH, "/grille_et_boucles.png");
	global.imageBoard = gtk_image_new_from_file(tmp);
	gtk_fixed_put(GTK_FIXED(fixed), global.imageBoard, 0, 0);


/*----------------------------------debut creation du menu-----------------------------------------------------------*/
	/* Creation du menu*/
	MenuBar = gtk_menu_bar_new(); /* barre de menu */

		/* menu "fichier" */
		Menu = gtk_menu_new();

			#ifndef CLIENT
			/* nouvelle partie */
			MenuItem = gtk_menu_item_new_with_label("Nouvelle partie");
			g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_new), &global);
			gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem); 

			/* sauvegarder */
			MenuItem = gtk_menu_item_new_with_label("Sauvegarder");
			g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_save), &global);
			gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem); 

			/* charger */
			MenuItem = gtk_menu_item_new_with_label("Charger une partie");
			g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_load), &global);
			gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);


			/* annuler */	
			MenuItem = gtk_menu_item_new_with_label("Annuler des coups");
			g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_cancel), &global);
			gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem); 

			/* revoir une partie */
			MenuItem = gtk_menu_item_new_with_label("Revoir une partie");
			g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_review), &global);
			gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem); 
			/* barre separatrice */
			MenuItem = gtk_separator_menu_item_new();
			gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
			#endif
			/* quitter*/
			MenuItem = gtk_menu_item_new_with_label("Quitter");
			g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_quit), &global);
			gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
			
			/* on met tout ce qui precede dans Fichier */
			MenuItem = gtk_menu_item_new_with_label("Jeu");
			gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), Menu);
			gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar), MenuItem);
			

		/* menu "Options" */  
		Menu = gtk_menu_new();

			/* sous-menu "Niveau" */
			subMenu = gtk_menu_new();

				/* sous menu choix du niveau */
				MenuItem = gtk_radio_menu_item_new_with_label(NULL, "facile");
				gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
				pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
				g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_change_level), &global);

				MenuItem = gtk_radio_menu_item_new_with_label(pList, "moyen");
				gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
				pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
				g_signal_connect(G_OBJECT(MenuItem) ,"activate", G_CALLBACK(disp_gtk_on_change_level), &global);

				MenuItem = gtk_radio_menu_item_new_with_label(pList, "difficile");
				gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
				pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
				g_signal_connect(G_OBJECT(MenuItem) ,"activate", G_CALLBACK(disp_gtk_on_change_level), &global);

				/* on met tout ce qui precede dans level */
				MenuItem = gtk_menu_item_new_with_label("niveau de jeu");
				gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);
				gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
				gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);

			/* sous menu choix du background */
			subMenu = gtk_menu_new();

				MenuItem = gtk_radio_menu_item_new_with_label(NULL, "desert");
				gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
				pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
				g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_change_background), &global);

				MenuItem = gtk_radio_menu_item_new_with_label(pList, "elephant");
				gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
				pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
				g_signal_connect(G_OBJECT(MenuItem) ,"activate", G_CALLBACK(disp_gtk_on_change_background), &global);

				/* on met tout ce qui precede dans image de fond */
				MenuItem = gtk_menu_item_new_with_label("image de fond");
				gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);
				gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
				gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);


			/* sous menu choix des pions */
			subMenu = gtk_menu_new();

				MenuItem = gtk_radio_menu_item_new_with_label(NULL, "wood");
				gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
				pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
				g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_change_pawns), &global);

				MenuItem = gtk_radio_menu_item_new_with_label(pList, "pion2");
				gtk_menu_shell_append(GTK_MENU_SHELL(subMenu), MenuItem);
				pList = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(MenuItem));
				g_signal_connect(G_OBJECT(MenuItem) ,"activate", G_CALLBACK(disp_gtk_on_change_pawns), &global);

				/* on met tout ce qui precede dans image de fond */
				MenuItem = gtk_menu_item_new_with_label("pion");
				gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);
				gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
				gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), subMenu);


			/* on met les deux sous menus dans Parametres*/
			MenuItem = gtk_menu_item_new_with_label("Options");
			gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), Menu);
			gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar), MenuItem);

		/* menu "Aide" */
		Menu = gtk_menu_new();
		
			/* A propos */  
			MenuItem = gtk_menu_item_new_with_label("A propos...");
			g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_about), global.mainWindow);
			gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);

			/* Rules */
			MenuItem = gtk_menu_item_new_with_label("Règles");
			g_signal_connect(G_OBJECT(MenuItem), "activate", G_CALLBACK(disp_gtk_on_rules), global.mainWindow);
			gtk_menu_shell_append(GTK_MENU_SHELL(Menu), MenuItem);
			
			/* Aide */
			MenuItem = gtk_menu_item_new_with_label("Aide");
			gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem), Menu);
			gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar), MenuItem);

/*-----------------------------------fin creation du menu------------------------------------------------------*/

#ifdef NETWORK
/*-----------------------------------creation du "chat"--------------------------------------------------------*/

	/* Creation du modele */
	global.listStore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
	/* Creation de la vue */
	listView = gtk_tree_view_new_with_model(GTK_TREE_MODEL(global.listStore));

	/* Creation de la premiere colonne */
	cellRenderer = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes("Nom",
		cellRenderer,
		"text", 0,
		NULL);

	/* Ajout de la colonne à la vue */
	gtk_tree_view_append_column(GTK_TREE_VIEW(listView), column);

	/* Creation de la deuxieme colonne */
	cellRenderer = gtk_cell_renderer_text_new();
	column = gtk_tree_view_column_new_with_attributes("Message",
		cellRenderer,
		"text", 1,
		NULL);

	/* Ajout de la colonne à la vue */
	gtk_tree_view_append_column(GTK_TREE_VIEW(listView), column);
	
	/* Ajout de la vue a la fenetre avec une scrollbar */
	scrollbar = gtk_scrolled_window_new(NULL, NULL);
		
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbar),
		GTK_POLICY_AUTOMATIC,
		GTK_POLICY_AUTOMATIC);
	gtk_container_add(GTK_CONTAINER(scrollbar), listView);
/*--------------------------------fin creation du chat---------------------------------------*/


/*------------------ creation de la zone de saisie des messages ----------------------------*/
	/* creation de la zone de saisie */
	global.entry = gtk_entry_new();
	/* message par default */
	gtk_entry_set_text(GTK_ENTRY(global.entry), "Entrez votre message ici");
	/* limitation de la taille du message */
	gtk_entry_set_max_length(GTK_ENTRY(global.entry), 100);
	
	g_signal_connect(G_OBJECT(global.entry), "activate", G_CALLBACK(disp_gtk_on_send_message), &global);


/*-------------- fin creation de la zone de saisie des messages ----------------------------*/
#endif

	/* initialisation de l'affichage */
	disp_gtk_init(fixed, Table, &global);
	/* Ajout du menu a la VBox */
	gtk_box_pack_start(GTK_BOX(VBox), MenuBar, FALSE, TRUE, 0);
	/* Ajout du fixed a la VBox */
	gtk_box_pack_start(GTK_BOX(VBox), fixed, TRUE, TRUE, 0);
	#ifdef NETWORK
	gtk_box_pack_start(GTK_BOX(VBox), scrollbar, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(VBox), global.entry, TRUE, TRUE, 0);
	#endif


	/* connexion des signaux */
	/* on ferme l'application ( disp_gtk_on_quit ) si mainWindows recoit un signal "destroy" ( croix en haut a droite ) */
	g_signal_connect(G_OBJECT(global.mainWindow), "destroy", G_CALLBACK(gtk_main_quit),
	NULL);

	/* Ajout de la VBox a le fenetre principale */
	gtk_container_add(GTK_CONTAINER(global.mainWindow), VBox);

	/* on affiche tous les widgets de la fenetre*/
	gtk_widget_show_all(global.mainWindow); 

	#ifdef NETWORK
	/* initialisation de ivy */
	#ifdef CLIENT
	IvyInit ("SURAKARTA_CLIENT", "client ready", 0, 0, 0, 0);
	/* bind des messages pour commencer une nouvelle partie */
	IvyBindMsg(on_ivy_new, &global, "^NewGame: (.*)");
	#endif
	#ifdef SERVER
	IvyInit ("SURAKARTA_SERVER", "server ready", 0, 0, 0, 0);
	#endif
	/* bind des messages de deplacement*/
	IvyBindMsg(on_ivy_move, &global, "^Move (.*) (.*) -> (.*) (.*)");
	/* bind des messages du chat */
	IvyBindMsg(on_ivy_chat, &global, "^Chat (.*) : (.*)");
	/* on lance le bus ivy*/
	IvyStart(bus);
	#endif
	/* boucle principale de gtk*/
	gtk_main(); 
	
	return(EXIT_SUCCESS);
}
